package com.gzhryc.shared_device.oem.h5.agent.controllers.api.v1;

import com.gzhryc.common.NumberTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.code.statistics.dao.db.UserProfitDayStatistics;
import com.gzhryc.shared_device.oem.code.statistics.dao.models.UserProfitSumMoneyData;
import com.gzhryc.shared_device.oem.code.statistics.services.UserProfitDayStatisticsService;
import com.gzhryc.shared_device.oem.code.statistics.services.dao.UserProfitDayStatisticsSearch;
import com.gzhryc.shared_device.oem.h5.agent.ClientAPICodeConstants;
import com.gzhryc.shared_device.oem.h5.agent.LoginUser;
import com.gzhryc.shared_device.oem.h5.agent.WebConstants;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.my.*;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MyStatisticsController {

    static Logger log = Logger.getLogger(MyStatisticsController.class);

    @Route(value = "/agent/api/v1/my/statistics/info.data")
    public MyStatisticsInfoResponse statistics(HttpServletRequest request) {
        MyStatisticsInfoResponse apiResp = new MyStatisticsInfoResponse(ClientAPICodeConstants.NULL_DATA,"暂无数据");
        LoginUser loginUser = WebUtils.getAttribute(request, WebConstants.ATTR_LOGIN_USER,LoginUser.class);
        if(loginUser != null){
            apiResp.setCode(ClientAPICodeConstants.SUCCESS,null);
            Date currentDate = new Date();
            UserProfitDayStatistics userProfitDayStatistics = UserProfitDayStatisticsService.self().getByKeys(loginUser.getId(),currentDate);
            if(userProfitDayStatistics == null){
                apiResp.getBody().setDayMoney("0");
            }else{
                Double temp = NumberTools.changeMoneyLi(userProfitDayStatistics.getIncomeMoney());
                apiResp.getBody().setDayMoney(NumberTools.getMoneyStr(temp));
            }
            //近7天
            Date startDate = DateUtils.addDays(currentDate,-7);
            UserProfitSumMoneyData sumMoneyData = UserProfitDayStatisticsService.self().sumByUserAndDate(loginUser.getId(),startDate,currentDate);
            if(sumMoneyData == null){
                apiResp.getBody().setWeekMoney("0");
            }else{
                Double temp = NumberTools.changeMoneyLi(sumMoneyData.getIncomeMoney());
                apiResp.getBody().setWeekMoney(NumberTools.getMoneyStr(temp));
            }
            //本月
            sumMoneyData = UserProfitDayStatisticsService.self().sumByUserAndMonth(loginUser.getId(),currentDate);
            if(sumMoneyData == null){
                apiResp.getBody().setMonthMoney("0");
            }else{
                Double temp = NumberTools.changeMoneyLi(sumMoneyData.getIncomeMoney());
                apiResp.getBody().setMonthMoney(NumberTools.getMoneyStr(temp));
            }
        }
        return apiResp;
    }

    @Route(value = "/agent/api/v1/my/statistics/profit/day/list.data")
    public MyStatisticsDayListResponse statisticsDayList(HttpServletRequest request) {
        MyStatisticsDayListResponse apiResp = new MyStatisticsDayListResponse(ClientAPICodeConstants.NULL_DATA, "暂无数据");
        MyStatisticsDayListRequest apiReq = WebUtils.getJsonRequest(request, MyStatisticsDayListRequest.class);
        if (apiReq != null) {
            LoginUser loginUser = WebUtils.getAttribute(request, WebConstants.ATTR_LOGIN_USER, LoginUser.class);
            if (loginUser != null) {
                Integer pageNo = apiReq.getBody().getPageNo() == null ? 1 : apiReq.getBody().getPageNo();
                String today = DateFormatUtils.format(new Date(), "yyyyMMdd");

                UserProfitDayStatisticsSearch search = new UserProfitDayStatisticsSearch();
                search.setUserId(loginUser.getId());

                List<UserProfitDayStatistics> profitDayStatisticsList = UserProfitDayStatisticsService.self().findBySearch(search, pageNo, WebConstants.PAGE_LIMIT);
                if (profitDayStatisticsList != null && profitDayStatisticsList.size() > 0) {
                    apiResp.setCode(ClientAPICodeConstants.SUCCESS, null);
                    apiResp.getBody().setItems(new ArrayList<>());
                    int i = 0;
                    if (pageNo == 1) {
                        UserProfitDayStatistics dayStatistics = profitDayStatisticsList.get(i);
                        if (dayStatistics.getDayDate().equals(today)) {
                            i = 1;
                            MyStatisticsDayListResponse.Item item = new MyStatisticsDayListResponse.Item();
                            item.setDayStr("今天");
                            item.setMoney(dayStatistics.getIncomeMoneyStr());
                            item.setIncomeCount(dayStatistics.getIncomeCount());
                            item.setRefundCount(dayStatistics.getRefundCount());
                            apiResp.getBody().getItems().add(item);
                        } else {
                            //今天没有数据
                            MyStatisticsDayListResponse.Item item = new MyStatisticsDayListResponse.Item();
                            item.setDayStr("今天");
                            item.setMoney("0");
                            apiResp.getBody().getItems().add(item);
                        }
                    }

                    for (; i < profitDayStatisticsList.size(); i++) {
                        UserProfitDayStatistics dayStatistics = profitDayStatisticsList.get(i);
                        MyStatisticsDayListResponse.Item item = new MyStatisticsDayListResponse.Item();
                        item.setDayStr(DateFormatUtils.format(dayStatistics.getCreateDate(), "yyyy年MM月dd日"));
                        item.setMoney(dayStatistics.getIncomeMoneyStr());
                        item.setIncomeCount(dayStatistics.getIncomeCount());
                        item.setRefundCount(dayStatistics.getRefundCount());
                        apiResp.getBody().getItems().add(item);
                    }
                }
            }
        }
        return apiResp;
    }

    @Route(value = "/agent/api/v1/my/statistics/profit/day.data")
    public MyStatisticsDayInfoResponse statisticsDay(HttpServletRequest request) {
        MyStatisticsDayInfoResponse apiResp = new MyStatisticsDayInfoResponse(ClientAPICodeConstants.NULL_DATA, "暂无数据");
        MyStatisticsDayInfoRequest apiReq = WebUtils.getJsonRequest(request, MyStatisticsDayInfoRequest.class);
        if (apiReq != null && apiReq.getBody().getDay() != null) {
            Date day = null;
            try {
                day = DateUtils.parseDate(apiReq.getBody().getDay(),"yyyy-MM-dd");
            } catch (ParseException e) {
                log.error(e.getMessage(),e);
            }
            if(day != null) {
                LoginUser loginUser = WebUtils.getAttribute(request, WebConstants.ATTR_LOGIN_USER, LoginUser.class);
                if (loginUser != null) {
                    UserProfitDayStatistics userProfitDayStatistics = UserProfitDayStatisticsService.self().getByKeys(loginUser.getId(),day);
                    if (userProfitDayStatistics != null) {
                        apiResp.setCode(ClientAPICodeConstants.SUCCESS, null);
                        apiResp.getBody().setOrderMoney(userProfitDayStatistics.getOrderMoneyStr());
                        apiResp.getBody().setIncomeCount(userProfitDayStatistics.getIncomeCount());
                        apiResp.getBody().setIncomeMoney(userProfitDayStatistics.getIncomeMoneyStr());
                        apiResp.getBody().setRefundCount(userProfitDayStatistics.getRefundCount());
                        apiResp.getBody().setRefundMoney(userProfitDayStatistics.getRefundMoneyStr());
                    }
                }
            }
        }
        return apiResp;
    }
}
